#include "jsonencode.h"
#include "ui_jsonencode.h"

#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonValue>
#include <QJsonParseError>

JsonEncode::JsonEncode(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::JsonEncode)
{
    ui->setupUi(this);
}

JsonEncode::~JsonEncode()
{
    delete ui;
}

void JsonEncode::on_jsonEncodeBt_clicked()
{
    //创建一个最外层对象
    QJsonObject obj;
    //创建data的对象
    QJsonObject dataObj;
    //在dataObj对象插入湿度
    dataObj.insert("shidu",QString("%1%").arg(ui->humLb->text()));

    //创建forecast对象
    QJsonObject forecastObj;
    forecastObj.insert("high",QString("高温 %1℃").arg(ui->highLb->text()));
    forecastObj.insert("low",QString("低温 %1℃").arg(ui->lowLb->text()));
    forecastObj.insert("notice",ui->noticeLb->text());

    //把forecast对象插入到dataobj中
    dataObj.insert("forecast",forecastObj);

    //把dataObj插入到最外层对象obj中
    obj.insert("data",dataObj);

    //把obj转为字符串
    QJsonDocument doc;
    doc.setObject(obj);
    QByteArray array = doc.toJson();
    ui->textBrowser->setText(QString(array));

}




















